//
//  DHS2_OrderCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/14.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_OrderCell.h"
#import "DHPayViewController.h"

@interface DHS2_OrderCellImageView : UIImageView

@property (strong, nonatomic) UILabel *statusLabel;

@end

@implementation DHS2_OrderCellImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if (!self.statusLabel) {
            self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 16, 16)];
            self.statusLabel.textColor = [UIColor whiteColor];
            self.statusLabel.textAlignment = NSTextAlignmentCenter;
            self.statusLabel.font = [UIFont systemFontOfSize:12];
            self.statusLabel.layer.cornerRadius = 8;
            self.statusLabel.clipsToBounds = YES;
            [self addSubview:self.statusLabel];
        }
    }
    return self;
}

@end

@interface DHS2_OrderCell()

@property (strong, nonatomic) NSDictionary *dic;

@end

@implementation DHS2_OrderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.payButton.layer.cornerRadius = self.payButton.height / 2;
    self.payButton.layer.borderWidth = 2;
    self.payButton.layer.borderColor = [[UIColor colorWithHexString:@"#E03E3E"] CGColor];
    
    WeakSelf(weakSelf)
    [self.payButton bk_addEventHandler:^(id sender) {
        __block CGFloat totalPrice = 0;
        NSArray *cost = [weakSelf.dic objectForKey:@"cost"];
        [cost enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CGFloat cost_value = [[obj objectForKey:@"value"] floatValue];
            totalPrice += cost_value;
        }];
        DHPayViewController *vc = [[DHPayViewController alloc] initWithPrice:[NSNumber numberWithFloat:totalPrice] order_id:[weakSelf.dic objectForKey:@"order_id"] size_id:@""];
        vc.payType = PayTypeOrder;
        [weakSelf.viewController.navigationController pushViewController:vc animated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateCell:(NSDictionary *)dic{
    self.dic = dic;
    self.statusLabel.text = [dic objectForKey:@"status"];
    NSDate *date = [NSDate dateWithServerISOString:[self.dic objectForKey:@"createdAt"] format:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    self.timeLabel.text = [NSDate stringFromDate:date withFormat:@"yyyy-MM-dd HH:mm"];
    if ([[dic objectForKey:@"status"] isEqualToString:@"待支付"]) {
        self.payButton.hidden = NO;
    }else{
        self.payButton.hidden = YES;
    }
    
    NSArray *logistics = [self.dic objectForKey:@"logistics"];
    if (logistics.count > 0) {
        NSDictionary *log = [logistics objectAtIndex:0];
        self.wuliuLabel.text = [log objectForKey:@"state"];
    }else{
        self.wuliuLabel.text = @"暂无物流信息";
    }
    
    
    NSArray *products = [self.dic objectForKey:@"products"];
    [self.scrollView removeAllSubviews];
    [products enumerateObjectsUsingBlock:^(NSDictionary *product, NSUInteger idx, BOOL * _Nonnull stop) {
        DHS2_OrderCellImageView *imgView = [[DHS2_OrderCellImageView alloc] initWithFrame:CGRectMake((5 + 65) * idx, 0, 65, self.scrollView.height)];
        if ([[product objectForKey:@"can_be_purchased"] boolValue] && ![[product objectForKey:@"can_be_leased"] boolValue]) {
            imgView.statusLabel.text = @"购";
            imgView.statusLabel.backgroundColor = [UIColor colorWithHexString:@"#5F657B"];
        }else{
            imgView.statusLabel.text = @"租";
            imgView.statusLabel.backgroundColor = [UIColor colorWithHexString:@"#F7BB18"];
        }
        [imgView dh_setImageURLString:[product objectForKey:@"image"]];
        [self.scrollView addSubview:imgView];
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
